package com.youdianstar.app.adapter

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.youdianstar.app.R
import com.youdianstar.app.databinding.LayoutItemPhoneCodeBinding
import com.youdianstar.app.entity.CodeEntity
import com.youdianstar.app.support.imp.CodeCallback


/**
 * File: PhoneCodeAdapter
 * Author: 82149 Create: 2021/9/27 15:24
 * Changes (from 2021/9/27)
 * --------------------------------------------------
 * description: 国家码适配
 * ---------------------------------------------------
 *
 */
class PhoneCodeAdapter : RecyclerView.Adapter<PhoneCodeAdapter.CodeVH>(){

    class CodeVH(binding: LayoutItemPhoneCodeBinding) : RecyclerView.ViewHolder(binding.root){
        private val mBinding = binding
        fun bind(data: CodeEntity){
            val title = data.title
            if (!TextUtils.isEmpty(title) && title != "-") {
                mBinding.tvTitle.visibility = View.VISIBLE
                mBinding.tvTitle.text = title
            } else {
                mBinding.tvTitle.visibility = View.GONE
            }
            mBinding.tvName.text = data.en
            mBinding.tvNum.text = "+" + data.code
//            val host = DataAppManager.instance.getConfig(ConfigKey.FILE_HOST)
//            if (!StrUtil.isEmpty(data.image)){
//                data.image?.let {
//                    val iconUrl = if (it.contains("http")) it else host + it
//                    bindImgGlide(mBinding.ivCountry, iconUrl)
//                }
//            } else
//                mBinding.ivCountry.setImageResource(R.mipmap.ic_launcher_round)
        }

        /**
         * Coil加载图片
         */
        fun bindImgGlide(viewImage: ImageView, url: String) {
            viewImage.load(url) {
                placeholder(R.mipmap.ic_code_default)
                error(R.mipmap.ic_code_default)
            }
        }
    }

    private var phoneCodeList: List<CodeEntity> = ArrayList()
    private var callback : CodeCallback? =null

    /**
     * 刷新数据
     */
    fun setData(codeTreeList: List<CodeEntity>) {
        this.phoneCodeList = codeTreeList
        println("PhoneCodeAdapter: 设置数据 ${codeTreeList.size} 条")
        notifyDataSetChanged()
    }

    fun setCallback(acCallback: CodeCallback){
        this.callback = acCallback
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CodeVH {
        val projectVH = CodeVH(
            LayoutItemPhoneCodeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
        projectVH.itemView.setOnClickListener {
            callback?.onItemSelected(phoneCodeList[projectVH.layoutPosition])
        }
        return projectVH
    }

    override fun onBindViewHolder(holder: CodeVH, position: Int) {
       holder.bind(phoneCodeList[position])

    }

    override fun getItemCount(): Int {
        return phoneCodeList.size
    }

}